// 封装wx.request()请求
export const request = (params) => {
  // 设置请求公共路径
  const baseURL = 'http://127.0.0.1:8360/api'
  // 从本地缓存中获取token
  const token = wx.getStorageSync('token')
  // 显示loading提示框
  wx.showLoading({
    title: '加载中',
    mask: true
  })

  return new Promise((resolve, reject) => {
    // 调用wx.request()方法
    wx.request({
      url: baseURL + params.url,
      data: params.data,
      header: {
        'content-type':'application/json',
        'X-Nideshop-Token': token
      },
      method: params.method,
      dataType: 'json',
      responseType: 'text',
      success: (result) => {
        resolve(result.data)
      },
      fail: (error) => {
        reject(error)
      },
      complete: () => {
        // 请求完成，关闭加载中提示框
        wx.hideLoading();
      }
    })
  })
}